#include "Sphere.h"


Sphere::Sphere (int rMax, int cMax, int stype) : Grid (rMax, cMax, stype)
{   
}

void Sphere::fill (void)
{
   if (structType == Vertex::type::P_N_AND_T) {
      //@TODO 
      coordDataNC = new Vertex::PNT[numCoords];     
   }
   else if (structType == Vertex::type::P_AND_N) {                  
      coordDataN = new Vertex::PN[numCoords];
      for (int j = 0; j <= rowMax; j++) {
         for (int i = 0; i <= colMax; i++) {
            int k = index (i, j);
            //r = 1
            float teta = ((float)i / colMax) * 2 * (float)Pi;
            float fi = ((float)j / rowMax)*(float)Pi;
            coordDataN[k].v = glm::vec3 (1.0f*(glm::cos (teta))*(glm::sin (fi)),
               1.0f*(glm::cos (fi)),
               1.0f*(glm::sin (teta))*(glm::sin (fi)));
            coordDataN[k].n = glm::normalize(2.0f*coordDataN[k].v);
            //std::cout << coordDataN[k].n.x << " " << coordDataN[k].n.y << " " << coordDataN[k].n.z << " " << std::endl;
         }
      }
   }
   else {                  
      coordData = new glm::vec3[numCoords];
      for (int j = 0; j <= rowMax; j++) {
         for (int i = 0; i <= colMax; i++) {
            int k = index (i, j);
            //r = 1
            float teta = ((float)i / colMax) * 2 * (float)Pi;
            float fi = ((float)j / rowMax)*(float)Pi;
            coordData[k] = glm::vec3 (1.0f*(glm::cos (teta))*(glm::sin (fi)),
                                 1.0f*(glm::cos (fi)),
                                 1.0f*(glm::sin (teta))*(glm::sin (fi)));
         }
      }      
   }
   int k = 0;
   indexData = new GLuint[numIndex];
   for (int j = 0; j < rowMax; j++) {
      for (int i = 0; i < colMax; i++) {
         indexData[k++] = index (i, j);
         indexData[k++] = index (i + 1, j + 1);
         indexData[k++] = index (i, j + 1);
         indexData[k++] = index (i, j);
         indexData[k++] = index (i + 1, j);
         indexData[k++] = index (i + 1, j + 1);
         //printf ("[%d] %d %d %d %d %d\n",k, indexData[k - 5], indexData[k - 4], indexData[k - 3], indexData[k - 2], indexData[k - 1]);
      }
   }
}
void Sphere::init (void)
{
   fill ();
   genBuffer ();
}
Sphere::~Sphere ()
{
}
